package com.shangxian.giveithome.controller;

import com.shangxian.giveithome.result.Result;
import com.shangxian.giveithome.entity.UserDetails;
import com.shangxian.giveithome.entity.seekhelp.*;
import com.shangxian.giveithome.annoaction.CheckLoginStatus;
import com.shangxian.giveithome.code.PetCode;
import com.shangxian.giveithome.exception.ExceptionUtils;
import com.shangxian.giveithome.service.SeekHelpService;
import com.shangxian.giveithome.utils.RedisUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.Date;

@Api(tags = "求助APi")
@RestController
@RequestMapping("/seekhelp")
public class SeekHelpController {

    @Autowired
    private SeekHelpService seekHelpService;

    @Autowired
    private RedisUtils redisUtils;

    @Autowired
    private HttpServletRequest httpServletRequest;


    /**
     * 查看求助详情
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "查看求助详情")
    @GetMapping(value = "/querySeekHelpDetailById/{id}")
    @CheckLoginStatus
    public Result querySeekHelpDetails(@PathVariable @NotNull(message = "主键不能为空") Long id) {
        return Result.success(seekHelpService.querySeekHelpDetailById(id));
    }


    /**
     * 用户查询求助列表
     *
     * @return
     */
    @ApiOperation(value = "用户查询求助列表")
    @GetMapping(value = "/querySeekHelpListByUser")
    @CheckLoginStatus
    public Result querySeekHelpListByUser(@PageableDefault(page = 0, size = 10) Pageable pageable) {
        UserDetails userDetails = redisUtils.getHeader(httpServletRequest);
        return Result.success(seekHelpService.queryByUserId(userDetails.getId(), pageable));
    }


    /**
     * 机构查询求助列表
     *
     * @return
     */
    @GetMapping(value = "/querySeekHelpListByMe")
    @CheckLoginStatus
    @ApiOperation(value = "机构查询求助列表")
    public Result querySeekHelpListByMe(@PageableDefault(page = 0, size = 10) Pageable pageable) {
        Long meManageId = redisUtils.getMechanismCheck(httpServletRequest);
        if (meManageId == -1L) {
            ExceptionUtils.error(PetCode.NOT_AUTH_UP_DATA);
        }
        return Result.success(seekHelpService.queryByMeId(meManageId, pageable));
    }

    /**
     * 创建求助记录
     *
     * @param seekHelpDTO
     * @return
     */
    @ApiOperation(value = "创建求助记录")
    @PostMapping(value = "/createSeekHelp")
    public Result createSeekHelp(@RequestBody @Validated SeekHelpDTO seekHelpDTO) {
        UserDetails userDetails = redisUtils.getHeader(httpServletRequest);
        seekHelpDTO.setUsId(userDetails.getId());
        seekHelpService.create(seekHelpDTO);
        return Result.success();
    }

    /**
     * 添加机构求助备注
     *
     * @param seekHelpDTO
     * @return
     */
    @ApiOperation(value = "添加机构求助备注")
    @PutMapping(value = "/updateMeRemarkSeekHelp")
    public Result updateMeRemarkSeekHelp(@RequestBody @Validated SeekHelpMeRemarkDTO seekHelpDTO) {
        Long aLong = redisUtils.getMechanismCheck(httpServletRequest);
        if (aLong == -1L) {
            ExceptionUtils.error(PetCode.NOT_AUTH_UP_DATA);
        }
        SeekHelpDO seekHelpDO = new SeekHelpDO();
        BeanUtils.copyProperties(seekHelpDTO, seekHelpDO);
        seekHelpDO.setUpdateTime(new Date());
//        seekHelpDO.setUpdateUsId(aLong);
        seekHelpService.updateMeRemark(seekHelpDO);
        return Result.success();
    }

    /**
     * 删除求助记录
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "删除求助记录")
    @DeleteMapping(value = "/deleteSeekHelp/{id}")
    public void deleteSeekHelp(@Valid @PathVariable @NotNull(message = "求助id不能为空") Long id) {
        Long aLong = redisUtils.getMechanismCheck(httpServletRequest);
        if (aLong == -1L) {
            ExceptionUtils.error(PetCode.NOT_AUTH_DEL_DATA);
        }
        seekHelpService.delete(id);
    }

    /**
     * 修改求助状态
     * @param seekHelpStatus
     * @return
     */
    @ApiOperation(value = "修改求助状态")
    @PutMapping("/updateSeekHelpStatus")
    @CheckLoginStatus
    public Result updateSeekHelpStatus(@RequestBody SeekHelpStatusDTO seekHelpStatus){
        Long aLong = redisUtils.getMechanismCheck(httpServletRequest);
        if (aLong == -1L) {
            ExceptionUtils.error(PetCode.NOT_AUTH_UP_DATA);
        }
        seekHelpService.updateStatus(seekHelpStatus);
        return Result.success();
    }

    /**
     * 添加求助募捐图片
     * @param seekHelpDImgDTO
     * @return
     */
    @ApiOperation(value = "添加求助募捐图片")
    @PutMapping("/updateSeekHelpDImg")
    @CheckLoginStatus
    public Result updateSeekHelpDImg(@RequestBody SeekHelpDImgDTO seekHelpDImgDTO){
        Long aLong = redisUtils.getMechanismCheck(httpServletRequest);
        if (aLong == -1L) {
            ExceptionUtils.error(PetCode.NOT_AUTH_UP_DATA);
        }
        seekHelpService.updateSeekHelpDImg(seekHelpDImgDTO);
        return Result.success();
    }
}
